package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetMealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = SetMealService.class)
@Transactional
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;
    @Autowired
    private JedisPool jedisPool;
    /**
     * 添加套餐基本信息和中间表
     * @param checkgroupIds
     * @param setmeal
     */
    @Override
    public void add(Integer[] checkgroupIds, Setmeal setmeal) {
          setMealDao.addDataBaseSetMeal(setmeal);
          this.addSetMealAndCheckGroud(checkgroupIds,setmeal.getId());
//          把数据图的reids放入
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
    }


    /**
     * 插入中间表
     * @param checkgroupIds
     * @param setMealId
     */
    private void addSetMealAndCheckGroud(Integer[] checkgroupIds,Integer setMealId) {
        for (Integer checkgroupId: checkgroupIds){
            HashMap<String, Object> map = new HashMap<>();
            map.put("checkgroupId", checkgroupId);
            map.put("setMealId", setMealId);
            setMealDao.addSetMealAndCheckGroud(map);
        }
    }

    /**
     * 分页查询套餐数据
     * @return
     */
    @Override
    public PageResult findAllSetMeal(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        String queryString=queryPageBean.getQueryString();
       if (queryString==null){
           queryString="";
       }
        Page <Setmeal> page=setMealDao.findByCondition("%"+queryString+"%");
        return  new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id--回显套餐基本信息
     * @param id
     * @return
     */
    @Override
    public Setmeal findSetMealById(Integer id) {
        return setMealDao.findSetMealById(id);
    }

    /**
     * 根据套餐id查看--勾选项目
     * @param id
     * @return
     */
    @Override
    public Integer[] findGroups(Integer id) {
       return setMealDao.findGroups(id);
    }

    /**
     * 更改套餐
     * @param checkgroupIds
     * @param setmeal
     */
    @Override
    public void edit(Integer[] checkgroupIds, Setmeal setmeal) {
//         1.更新套餐基本信息表
        setMealDao.updateBaseSetMeal(setmeal);

       // 2. 删除中间表
        setMealDao.deleteSetMealAndCheckGroup(setmeal.getId());
//         3.插入中间表
        for (Integer checkgroupId:checkgroupIds){
            HashMap<String, Object> map = new HashMap<>();
            map.put("checkgroupId", checkgroupId);
            map.put("setmealId", setmeal.getId());
            setMealDao.addSetMealAndCheckGroup(map);
        }
    }

    /**
     * 查找全部套餐数据
     * @return
     */
    @Override
    public List<Setmeal> getSetmeal() {
       return setMealDao.getSetmeal();
    }

    /**
     * 寻找套餐名字和套餐预约人数
     * @return
     */
    @Override
    public List<Map<String, Object>> findOrderCountSetmeal() {
        return setMealDao.findOrderCountSetmeal();
    }
}
